**************************************************************
***** Name:        do_analysis_indiv.do      		     *****
***** Description: This do file analyzes data at the	 *****
***** individual level.									 *****
**************************************************************

clear all
global path "C:\Users\patry\OneDrive\Desktop\Mario replication files"
cd "$path"
set more off

use "Data/clean data/clean_indiv.dta", clear

global controls_p2 "round_diff skills"
global controls_p3 $controls_p2 phase2score_indiv
global myopts "bf(a2) sfmt(a2) r2 se label prehead(\begin{tabular}{l*{@M}{c}}) postfoot(\end{tabular}) mlabels(, prefix() suffix() depvars) noisily noeqlines nonumbers varlabels(_cons Constant, end("" ) nolast)  starlevels(* 0.1 ** 0.05 *** 0.01)"


****************************************************
****************************************************
****************************************************
**********        MAIN TEXT TABLES 	      ********** 
****************************************************
****************************************************
****************************************************


************************************************************
*	TABLE 1B: SUMMARY STATISTICS (PARTICIPANT-LEVEL) 	   *
************************************************************
 
 
estpost sum game1 game2 game3 game4  if round==1, d
est store a 
esttab a using Output/Tables/Table1A.tex,  replace cells("mean (label(Mean) fmt(2)) sd (label(Std.Dev) fmt(2))  min (label(Min) fmt(2)) p25 (label(P25) fmt(2)) p50 (label(Median) fmt(2)) p75 (label(P75) fmt(2)) max (label(Max) fmt(2)) count (label(N) fmt(0))")   noobs nomtitle nonumber varlabels(`e(labels)') varwidth(20) 


************************************************************
*	TABLE 4: IMPACT ON TRUST, EFFORT, AND AI ATTITUDES 	   *
************************************************************
 
la var z_trust "Trust index"
la var z_effort "Effort index"
la var z_AIattitude "AI attitudes index"

qui reg z_trust  ai_partner ai_spillover newhire_player newhire_partner newhire_spillover tacit  i.round $controls_p2 if sample==1 & round==12, r cluster(team_id)
	estimates store trust 
qui reg z_effort  ai_partner ai_spillover newhire_player newhire_partner newhire_spillover tacit  i.round $controls_p2 if sample==1 & round==12, r cluster(team_id)
	estimates store effort
qui reg z_AIattitude  ai_partner ai_spillover newhire_player newhire_partner newhire_spillover tacit  i.round $controls_p2 if sample==1 & round==12, r cluster(team_id)
	estimates store ai 
esttab  trust effort ai using Output/Tables/Table4.tex,  $myopts keep( ai_partner ai_spillover newhire_player newhire_partner newhire_spillover)   stats(r2 N , label("R2" "Observations" )  fmt(%9.3f 0))  replace


****************************************************
****************************************************
****************************************************
**********        MAIN TEXT FIGURES	      ********** 
****************************************************
****************************************************
****************************************************
	

**********************************************************************
*	  FIGURE 1: KERNEL DENSITY PLOT OF PERFORMANCE, AI VS HUMANS     *
**********************************************************************


preserve
	keep if round==6 & sample==1
	keep ingred 
	gen human=1

	append using "Data/raw data/AI_Scores.dta"
	gen type = 1
	replace type=2 if ai==1

	sum ingred if human==1
		 local mean_human = r(mean)	 
	sum ingred if ai==1
		 local mean_ai = r(mean)
		  
	twoway (kdensity ingred if type==2, lcolor(black)) (kdensity ingred if type==1, lcolor(gs9)),  legend(order( 1 "AI" 2 "Human")) ///
	graphregion(color(white)) bgcolor(white) xlabel(0(1)12) xtitle("Total ingredients returned") ytitle("Density")  xline(`mean_human', lpattern(dash) lcolor(gs9)) xline(`mean_ai', lpattern(dash) lcolor(black)) ///
		 text(.4 8.6 "AI mean: 7.5") text( .4 4.9 "Human mean: 6.4", color(gs9))
	graph export "Output/Figures/Figure1.pdf", replace
	
restore

  
****************************************************
****************************************************
****************************************************
**********       APPENDIX TABLES 	      ********** 
****************************************************
****************************************************
****************************************************


**************************************************************************
*	   TABLE B2: EFFECT OF TACIT MANIPULATION ON EXPLICIT COMMUNICATION  *
**************************************************************************


foreach y in z_explicit talkcoordinate preciserules explicitplan {
	reg `y'  tacit   i.round $controls_p2 if sample==1 & round==6, r cluster(team_id)
		estimates store `y'
}
esttab  z_explicit talkcoordinate preciserules explicitplan  using Output/Tables/Appendix/TableB2.tex,  $myopts keep( tacit)   stats(r2 N , label("R2" "Observations" )  fmt(%9.3f 0))  replace


************************************************************************************
*	   TABLE B5: IMPACT ON TRUST, EFFORT, AND AI ATTIITUDES, BY TACIT VS EXPLICIT  *
************************************************************************************


foreach g in ai_player ai_partner ai_spillover newhire_player newhire_partner newhire_spillover {
	gen `g'Xtacit = `g' * tacit 
}

qui reg z_trust  ai_partner ai_partnerXtacit ai_spillover ai_spilloverXtacit newhire_player newhire_playerXtacit  newhire_partner newhire_partnerXtacit newhire_spillover newhire_spilloverXtacit tacit  i.round $controls_p2 if sample==1 & round==12, r cluster(team_id)
	estimates store trust 
qui reg z_effort  ai_partner ai_partnerXtacit ai_spillover ai_spilloverXtacit newhire_player newhire_playerXtacit  newhire_partner newhire_partnerXtacit newhire_spillover newhire_spilloverXtacit tacit  i.round $controls_p2 if sample==1 & round==12, r cluster(team_id)
	estimates store effort 
qui reg z_AIattitude ai_partner ai_partnerXtacit ai_spillover ai_spilloverXtacit newhire_player newhire_playerXtacit  newhire_partner newhire_partnerXtacit newhire_spillover newhire_spilloverXtacit tacit  i.round $controls_p2 if sample==1 & round==12, r cluster(team_id)
	estimates store attitude
esttab trust effort attitude using Output/Tables/Appendix/TableB5.tex,  $myopts keep(ai_partner ai_partnerXtacit ai_spillover ai_spilloverXtacit newhire_player newhire_playerXtacit  newhire_partner newhire_partnerXtacit newhire_spillover newhire_spilloverXtacit tacit )   stats(r2 N , label("R2" "Observations" )  fmt(%9.3f 0))  replace


*****************************************
*	   TABLE B6: EFFECT ON TRUST ITEMS  *
*****************************************


foreach y in partnerskill partnereffort partnertrust {
	reg `y'  ai_partner ai_spillover newhire_player newhire_partner newhire_spillover tacit  i.round $controls_p2 if sample==1 & round==12, r cluster(team_id)
		estimates store `y'
}
esttab partnerskill partnereffort partnertrust using Output/Tables/Appendix/TableB6.tex,  $myopts keep( ai_partner ai_spillover newhire_player newhire_partner newhire_spillover)   stats(r2 N , label("R2" "Observations" )  fmt(%9.3f 0))  replace

 
*****************************************
*	   TABLE B7: EFFECT ON EFFORT ITEMS *
*****************************************


foreach y in  owneffort noattention owntrybest {
	reg `y'  ai_partner ai_spillover newhire_player newhire_partner newhire_spillover tacit  i.round $controls_p2 if sample==1 & round==12, r cluster(team_id)
		estimates store `y'
}
esttab  owneffort noattention owntrybest using Output/Tables/Appendix/TableB7.tex,  $myopts keep( ai_partner ai_spillover newhire_player newhire_partner newhire_spillover)   stats(r2 N , label("R2" "Observations" )  fmt(%9.3f 0))  replace


*****************************************
*	   TABLE B8: EFFECT ON AI ATTITUDES *
*****************************************


foreach y in  AIpositiveimpact preferhuman uncomfortAIvehicle {
	reg `y'  ai_partner ai_spillover newhire_player newhire_partner newhire_spillover tacit  i.round $controls_p2 if sample==1 & round==12, r cluster(team_id)
		estimates store `y'
}
esttab  AIpositiveimpact preferhuman uncomfortAIvehicle using Output/Tables/Appendix/TableB8.tex,  $myopts keep( ai_partner ai_spillover newhire_player newhire_partner newhire_spillover)   stats(r2 N , label("R2" "Observations" )  fmt(%9.3f 0))  replace


************************************************************************************
*	   TABLE B9: IMPACT ON TRUST, EFFORT, AND AI ATTIITUDES, BY TACIT VS EXPLICIT  *
************************************************************************************


qui reg z_trust  ai_partner ai_partnerXtacit ai_spillover ai_spilloverXtacit newhire_player newhire_playerXtacit  newhire_partner newhire_partnerXtacit newhire_spillover newhire_spilloverXtacit tacit  i.round $controls_p2 if sample==1 & round==12, r cluster(team_id)
	estimates store trust 
qui reg z_effort  ai_partner ai_partnerXtacit ai_spillover ai_spilloverXtacit newhire_player newhire_playerXtacit  newhire_partner newhire_partnerXtacit newhire_spillover newhire_spilloverXtacit tacit  i.round $controls_p2 if sample==1 & round==12, r cluster(team_id)
	estimates store effort 
qui reg z_AIattitude ai_partner ai_partnerXtacit ai_spillover ai_spilloverXtacit newhire_player newhire_playerXtacit  newhire_partner newhire_partnerXtacit newhire_spillover newhire_spilloverXtacit tacit  i.round $controls_p2 if sample==1 & round==12, r cluster(team_id)
	estimates store attitude
esttab trust effort attitude using Output/Tables/Appendix/TableB9.tex,  $myopts keep(ai_partner ai_partnerXtacit ai_spillover ai_spilloverXtacit newhire_player newhire_playerXtacit  newhire_partner newhire_partnerXtacit newhire_spillover newhire_spilloverXtacit tacit )   stats(r2 N , label("R2" "Observations" )  fmt(%9.3f 0))  replace


************************************************************************************
*	   TABLE B10: CORRELATION BETWEEN INDIVIDUAL SKILL AND INDIVIDUAL INGREDIENTS  *
************************************************************************************


qui reg ingred skills, r cluster(team_id) 
	 estimates store t1
	 estadd local roundfe "No"
	 estadd local treatment "No"
	 estadd local teamfe "No"
qui reg ingred skills i.round, r cluster(team_id) 
	 estimates store t2
	 estadd local roundfe "Yes"
	 estadd local treatment "No"
	 estadd local teamfe "No"
qui reg ingred skills i.round tacit ai newhire, r cluster(team_id) 
	 estimates store t3
	 estadd local roundfe "Yes"
	 estadd local treatment "Yes"
	 estadd local teamfe "No"
qui areg ingred skills  i.round, r cluster(team_id) absorb(team_id)
	 estimates store t4
	 estadd local roundfe "Yes"
	 estadd local treatment "No"
	 estadd local teamfe "Yes"
esttab t1 t2 t3 t4 using Output/Tables/Appendix/TableB10.tex,  keep(skills) se starlevels( * 0.10 ** 0.05 *** 0.010) stats(r2 N roundfe treatment teamfe , label("R2" "Observations" "Round f.e" "Treatment" "Team f.e.")  fmt(%9.3f 0 %9.3f 0 %9.3f %9.3f ))  replace

 
****************************************************************
*	   TABLE B12: IMPACT OF TEAM ASSIGNMENT ON AI PERFORMANCE  *
****************************************************************
 

cap drop z_game_p234_high
gen z_game_p234_high = 0
replace z_game_p234_high = 1 if inlist(team_id, "021312", "021410" "021412" "022811", "030503", "101602", "101603", "102412", "103110", "110511")
replace z_game_p234_high = 1 if inlist(team_id, "110801", "110811", "111911", "112205", "112501", "112503", "112504", "112601", "112611")

qui reg ingred z_game_p234_high  if player==0, r cluster(team_id)
  estadd local roundctrl "No"
  estimates store AI_1
qui  reg ingred z_game_p234_high round_diff i.round if player==0, r cluster(team_id)
  estimates store AI_2
  estadd local roundctrl "Yes"
esttab AI_1 AI_2 using Output/Tables/Appendix/TableB12.tex,  keep(z_game_p234_high) se starlevels( * 0.10 ** 0.05 *** 0.010) stats(r2 N roundctrl , label("R2" "Observations" "Round controls")  fmt(%9.3f 0 %9.3f 0 %9.3f %9.3f ))  replace
  
  
*********************************************************************************
*	   TABLE B13: IMPACT OF PARTNER ASSIGNMENT ON HIGH-SKILLED TEAM PERFORMANCE *
*********************************************************************************
 
 
cap drop z_game_p234_high
gen z_game_p234_high = 0
replace z_game_p234_high = 1 if inlist(team_id, "021312", "021410" "021412" "022811", "030503", "101602", "101603", "102412", "103110", "110511")
replace z_game_p234_high = 1 if inlist(team_id, "110801", "110811", "111911", "112205", "112501", "112503", "112504", "112601", "112611")
 
qui reg ingred ai  if z_game_p234_high==1 & player!=1 & newhire==0 & phase3==1, r cluster(team_id)
   estimates store highskill1
   estadd local roundctrl "No"
qui reg ingred ai round_diff i.round if z_game_p234_high==1 & player!=1 & newhire==0 & phase3==1, r cluster(team_id)
   estimates store highskill2
   estadd local roundctrl "Yes"
esttab highskill1 highskill2 using Output/Tables/Appendix/TableB13.tex,  keep(ai) se starlevels( * 0.10 ** 0.05 *** 0.010) stats(r2 N roundctrl , label("R2" "Observations" "Round controls")  fmt(%9.3f 0 %9.3f 0 %9.3f %9.3f ))  replace
  

****************************************************
****************************************************
****************************************************
**********       APPENDIX FIGURES 	      ********** 
****************************************************
****************************************************
****************************************************


***************************************************
*	   FIGURE  A.2: DISTRIBUTION OF PERFORMANCE   *
***************************************************

preserve

	keep if round==6 & sample==1
	keep ingred 
	gen human=1

	append using "Data/raw data/AI_Scores.dta"
	gen type = 1
	replace type=2 if ai==1

	cdfplot ingred,by(type) legend( rows(1) label(1 "Human") label(2 "AI")) opt1( lc(blue black) ) ///
	graphregion(color(white)) bgcolor(white) xlabel(0(1)12) xtitle("Total ingredients returned")
	graph export "Output/Figures/Appendix/FigureA2.pdf", replace
	
restore


*********************************************************
*	   FIGURE  B.7: AI PERFORMANCE BY TEAM ASSIGNMENT   *
*********************************************************

 
 cap drop z_game_p234_high
 gen z_game_p234_high = 0
 replace z_game_p234_high = 1 if inlist(team_id, "021312", "021410" "021412" "022811", "030503", "101602", "101603", "102412", "103110", "110511")
 replace z_game_p234_high = 1 if inlist(team_id, "110801", "110811", "111911", "112205", "112501", "112503", "112504", "112601", "112611")

 * How many ingredients does the AI return in high vs medium vs low firmm?
  sum ingred if player==0 & z_game_p234_high==0
  local mean_lm = round(r(mean), .01)
  sum ingred if player==0 & z_game_p234_high==1
  local mean_h = round(r(mean), .01)

 *histogram
 twoway (histogram ingred if player==0 & z_game_p234_high==1, percent width(.5)  fcolor(none) lcolor(black) ) ///
 (histogram ingred if player==0 & z_game_p234_high==0, percent width(.48)  fcolor(none) lcolor(gs12) ) , ///
 legend(order( 1 "High skill team" 2 "Low/Middle skill team")) xtitle("Ingredients returned by AI")  graphregion(color(white)) bgcolor(white) xline(`mean_h', lcolor(black))  xline(`mean_lm', lcolor(gs10)) text(40 7.2 "High skill mean = `mean_h'") text(38 7.46 "Low/middle skill mean = `mean_lm'", color(gs10))
  graph export "Output/Figures/Appendix/FigureB7.pdf", replace
  
  
**************************************************************************
*	   FIGURE  B.8 HIGH-SKILLED TEAM PERFORMANCE BY PARTNER ASSIGNMENT   *
**************************************************************************


 cap drop z_game_p234_high
 gen z_game_p234_high = 0
 replace z_game_p234_high = 1 if inlist(team_id, "021312", "021410" "021412" "022811", "030503", "101602", "101603", "102412", "103110", "110511")
 replace z_game_p234_high = 1 if inlist(team_id, "110801", "110811", "111911", "112205", "112501", "112503", "112504", "112601", "112611")

 sum ingred if  z_game_p234_high==1 & player!=1 & ai==1 & phase3==1
 local mean_ai = round(r(mean),.01)
 sum ingred if  z_game_p234_high==1 & player!=1 & control==1 & phase3==1
 local mean_ctrl = round(r(mean),.01)
 
 twoway (histogram ingred if  z_game_p234_high==1 & player!=1 & ai==1 & phase3==1, percent width(.5)  fcolor(none) lcolor(black) ) ///
 (histogram ingred if  z_game_p234_high==1 & player!=1 & control==1 & phase3==1, percent width(.45)  fcolor(none) lcolor(gs12) ) , ///
 legend(order( 1 "AI partner" 2 "Human partner")) xtitle("Ingredients returned by high skilled team")  graphregion(color(white)) bgcolor(white) xline(`mean_ai', lcolor(black)) xline(`mean_ctrl', lcolor(gs10)) text(29 7.80 "AI mean = `mean_ai'") text(29 4.3 "Human partner mean = `mean_ctrl'", color(gs10))
  graph export "Output/Figures/Appendix/FigureB8.pdf", replace
 
  
*********************************************
*	   FIGURE  B.10: COWORKER PREFERENCES   *
*********************************************

 
histogram preferhuman,  xtitle("I prefer to perform this task with a human over AI (7=strongly agree)") percent graphregion(color(white)) bgcolor(white)   xscale(range(1 7 )) xlabel(1(1)7) discrete width(.5)     color(navy) 
  graph export "Output/Figures/Appendix/FigureB10.png", replace


*******************************************	
*******************************************
*******************************************
*	   END OF INDIV TABLES AND FIGURES    *
*******************************************
*******************************************
*******************************************
	